package com.boarsoft.boar.demo.user.biz;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.demo.user.entity.PocAccount;
import com.boarsoft.dao.sharding.bean.DaoSharding;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("accountBiz")
public class PocAccountBizImpl extends SimpleBizImpl implements PocAccountBiz{

	@Override
	@Transactional
	@DaoSharding(policy = "tenant", index = 0)
	public boolean add(int tenantId, PocAccount account) {
		if (dao.save(account) == null) {
			return false;
		}
		return true;
	}

	@Override
	@Transactional
	@DaoSharding(policy = "tenant", index = 0)
	public boolean update(int tenantId, PocAccount account) {
		
		return dao.update(account);
		
	}
	
	@Override
	@Transactional(readOnly = true)
	@DaoSharding(policy = "tenant", index = 0)
	public PocAccount findByUser(int tenantId, int userId) {
		
		StringBuilder sb = new StringBuilder();
		sb.append("and tenant_id = ").append(tenantId);
		sb.append(" and user_id = ").append(userId).append("");
		
		return dao.find(PocAccount.class, sb.toString());
		
//		String hql = "from PocAccount where userId=" + userId;
//		return (PocAccount) dao.find(hql);
		
	}

}
